<?php
//设置编码为UTF-8，以避免中文乱码
header('Content-Type:text/html;charset=utf-8');
error_reporting(E_ERROR);

$path = "../../../Uploads/video/";
if(!file_exists($path)) {
    mkdir($path);
}
if(file_exists($path . $_FILES["file"]["name"])){ 
    echo "exist";
    exit;
}
$total = $_POST["chunks"];
$index = $_POST["chunk"]+1;
if(!$total){
    $name=iconv("UTF-8","gb2312", $_FILES["file"]["name"]);
    $res = move_uploaded_file($_FILES["file"]["tmp_name"], $path. $name);
    $name=iconv("gb2312","UTF-8", $name);
    var_dump($res);
    echo "Stored in: " . $path. $name;
    exit;
}

if ($_FILES["file"]["error"] > 0){
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}else{
    $name=iconv("UTF-8","gb2312", $_FILES["file"]["name"])."_".$index.".temp";
    $res = move_uploaded_file($_FILES["file"]["tmp_name"], $path. $name);
    $name=iconv("gb2312","UTF-8", $name);
}

if($total == $index){
    $name=iconv("UTF-8","gb2312", $_FILES["file"]["name"]);
    $file = fopen($path . $name, 'wb');
    $count = $total+1;
    for ($i=1; $i < $count; $i++) {
        echo "ii:".$i;
        $t_name = $name."_".$i.".temp";
        $f_cache =  fopen($path.$t_name, "r");
        $f_size = filesize($path.$t_name);
        $f_content = fread($f_cache, $f_size);
        fclose($f_cache);
        unlink($path.$t_name);
        fwrite($file, $f_content); 
    }
    fclose($file);
}
?>